package com.scanner.util;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.WindowManager;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 获取设备信息的工具
 */
public class HardwareInfoUtill {

    private static HardwareInfoUtill infoUtill = null;
    private Context mContext;
    private HardwareInfoUtill(Context mContext) {
        this.mContext = mContext;
    }

    public static synchronized HardwareInfoUtill getInstance(Context context){
        if(infoUtill == null){
            infoUtill = new HardwareInfoUtill(context.getApplicationContext());
        }
        return infoUtill;
    }

    /**
     * Serial Number
     *  获取设备号
     * @return
     */
    @SuppressLint("MissingPermission")
    public String getSN() {
        String SN = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            SN = Build.getSerial();
        }else {
            try {//目前验证，读属性的方式在bayt\cht\mtk都可行
                Class<?> c =Class.forName("android.os.SystemProperties");
                Method get =c.getMethod("get", String.class);
                String invoke = (String) get.invoke(c, "ro.serialno");
                if(!TextUtils.isEmpty(invoke)){
                    SN = invoke;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return SN;
    }

    /**
     * 设备号model number
     *
     * @return
     */
    public String getModelNumber() {

        String model = "";
        try {
            Method systemProperties_get = Class.forName(
                    "android.os.SystemProperties").getMethod("get",
                    String.class);
            if ((model = (String) systemProperties_get.invoke(null,
                    "ro.product.model")) != null)
                return model;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }

    /**
     * 获取设备sim信息(手机号)
     */
    @SuppressLint("MissingPermission")
    public String getSim() {
        String mNumber = "";
        TelephonyManager mTelManager = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        mNumber = mTelManager.getLine1Number();

        if (TextUtils.isEmpty(mNumber)) {
            return "";
        }
        return mNumber;
    }

    /**
     * 获取cpu 型号信息
     */
    public String getCpuModelString() {

        try {
            StringBuffer buffer = new StringBuffer();
            String ret = "";
            RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");
            do {
                ret = reader.readLine();
                if ((!TextUtils.isEmpty(ret))
                        && (ret.contains("Processor") || ret
                        .contains("model name"))) {
                    break;
                }
            } while (ret != null);
            reader.close();
            reader = null;

            if (!TextUtils.isEmpty(ret)) {
                String[] model_name = ret.split(":");
                LogUtil.w("cpu="+ Arrays.toString(model_name));
                if (model_name.length == 2) {
                    return model_name[1].trim();
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return "";
    }


    /**
     * 获取IMEI(手机序列号)
     *
     * @return 如果IEMI不可得，则返回空字符
     */
    @SuppressLint("MissingPermission")
    public String getImei() {
        String mImei = "";
        TelephonyManager mTelManager = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        mImei = mTelManager.getDeviceId();

        if (TextUtils.isEmpty(mImei)) {
            return "null";
        } else {
            return mImei;
        }

    }

    /**
     * 获取蓝牙 MAC
     * 有一个傻逼客户，在intel上不能出现蓝牙功能
     */
    public String getBtMac() {
        String mResult = "";
        BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBtAdapter != null) {
            if (!mBtAdapter.isEnabled()) {
                mBtAdapter.enable();
            }
            mResult = mBtAdapter.getAddress();
        }
        return mResult;
    }

    public String getWifiSSID(){
        WifiManager wifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = null;
        if(wifiManager!=null){
            wifiInfo = wifiManager.getConnectionInfo();
            return wifiInfo == null ? "" : wifiInfo.getSSID();
        }
        return null;
    }

    /**
     * 获取app 版本信息
     */
    public String getAppVersionName() {

        PackageManager packageManager = mContext.getPackageManager(); // getPackageName()是你当前类的包名，0代表是获取版本信息

        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(
                    mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = "";
        if(packInfo!=null){
            version = packInfo.versionName;
        }
        return version;
    }

    /**
     * 获取wifi MAC
     * (待完善，需要离线也可以获取mac)
     */
   /* public String getWifiMac() {

        String mResult = "";
        WifiManager mWifi = (WifiManager) mContext.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        if (!mWifi.isWifiEnabled()) {
            mWifi.setWifiEnabled(true);
        }

        if(NetworkUtil.isWifiConnected(mContext)){
            WifiInfo wifiInfo = mWifi.getConnectionInfo();
            mResult = wifiInfo.getMacAddress();
        }
        return mResult;
    }*/

    /**
     * 出厂系统版本
     *ro.product.version       M3出货后全部统一使用这个字段
     * ro.build.display.id      默认的
     * @return
     */
    public String getSystemVersion() {
        String version = "";
        try {
            Method systemProperties_get = Class.forName(
                    "android.os.SystemProperties").getMethod("get",
                    String.class);
            if ((version = (String) systemProperties_get.invoke(null,
                    "ro.product.version"/*"ro.product.version"*/)) != null)
                return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取屏幕宽、高
     */
    public String getScreenDisplayPix() {
        int mSzie[] = new int[] { 0, 0 };
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        Point outSize = new Point();
        wm.getDefaultDisplay().getSize(outSize);
        mSzie[0] = outSize.x;
        mSzie[1] = outSize.y;
        return mSzie[0] + "*" + mSzie[1];
    }

    public String getDeviceNum() {
        String version = "";
        try {
            Method systemProperties_get = Class.forName(
                    "android.os.SystemProperties").getMethod("get",
                    String.class);
            if ((version = (String) systemProperties_get.invoke(null,
                    "ro.product.number"/*"ro.product.version"*/)) != null)
                return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
